using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine.UI;

namespace Gj
{
    public class TimeHelper : NodeHelper
    {
        public bool pro;
        public bool online;
        public bool local;
        public List<ShowTimeItem> list;
        private Text _Text;
        private TextMeshProUGUI _TextPro;

        private Text Text
        {
            get
            {
                if (_Text == null) _Text = GetComponent<Text>();
                return _Text;
            }
        }

        private TextMeshProUGUI TextPro
        {
            get
            {
                if (_TextPro == null) _TextPro = GetComponent<TextMeshProUGUI>();
                return _TextPro;
            }
        }

        protected override BindDataType bindDataType => BindDataType.Int;

        protected override void BindData(int t)
        {
            var time = DateTools.GetNowTime() - t;
            for (var i = 1; i < list.Count; i++)
                if (time < list[i].second)
                {
                    SetText(list[i - 1].text);
                    return;
                }

            SetText(list[list.Count - 1].text);
        }

        private void SetText(string t)
        {
            var text = GetText(t);
            if (pro)
                TextPro.text = text;
            else
                Text.text = text;
        }

        private string GetText(string s)
        {
            // Debug.LogErrorFormat("{0} {1} {2}", gameObject.name, gameObject.transform.parent.name ,s);
            if (online && !StringTools.IsEmpty(s))
            {
                var o = Localization.Instance.GetOnlineValue(s);
                s = o == "" ? s : o;
            }

            return s;
        }

        [Serializable]
        public class ShowTimeItem
        {
            public int second;
            public string text;
        }
    }
}